<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />

<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<!--<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">-->
<title>角色权限</title>
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

<jsp:include page="../common/import.jsp"></jsp:include>

</head>
<body>
 <div class="content pt0">
              <div class="right-cot w100" style="padding-left:20px;">
                  
                  <input type="hidden" value="${roleId }" id="_roleId" />
                  <div class="form-top">
                        <div class="mb10">
                           应用： <select id="_appId" name="appId" class="js-example-basic-single form-control-select" onchange="changeRoleUnits()" >
                                      
										<c:forEach items="${appList}" var="a">
											<option value="${a.id }"  >${a.appName }</option>
										</c:forEach>
                                  </select>
                                  <c:if test="${empty appList }">
                                  <span style="color:red">请先给角色赋应用</span>
                                  </c:if>
                        </div>
                  </div>
                  <div class="table-scroll" id="unitDetail">
                      <ul id="unitTree" class="ztree"></ul>
                  </div>
                  <div class="form-top">
                    <button id="saveBtn" class="btn btn-orange" onclick="saveRoleUnit(this)" ><i class="fa fa-save mr5"></i>保存</button>
            	    <button class="btn btn-default" onclick="closeLayerOpen()" ><i class="fa fa-reply mr5"></i>返回</button>
                  </div>
              </div>
          <!--main end--> 
      </div>     
</body>
<script type="text/javascript">
baseUrl = '${ctx}';

var roleUnit_setting = {
		check: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true
			}
		}
	};
$(document).ready(function(){
	changeRoleUnits();
	$(".js-example-basic-single").select2();
});

function changeRoleUnits(){
	var appId = $.trim($('#_appId').val());
	var roleId = $.trim($('#_roleId').val());
	if(appId == null || appId == '' || roleId == ''){
		$("#saveBtn").attr('disabled','disabled');
		return;
	}
	
	
	$("#saveBtn").remove('disabled');
	
	$("#unitTree").empty();
	$.post(baseUrl+'/role/query/app/units',{roleId:roleId,appId:appId},function(result){
		var zNodes = [];
		if(result.code =='success'){
			zNodes = result.data;
		}else{
			warnTip("系统错误，加载失败");
		}
		showUnitTree(zNodes);
	},'json');
}

function showUnitTree(zNodes){
	$("#unitTree").empty;
	$.fn.zTree.init($("#unitTree"), roleUnit_setting, zNodes);
	setCheck();
	
}
var isSubmiting_roleUnit = false;
function saveRoleUnit(btn){
	if(isSubmiting_roleUnit) return;
	//获取所有已选择的
	var zTree = $.fn.zTree.getZTreeObj("unitTree");
	var nodes=zTree.getCheckedNodes(true);
	var roleUnit = {};
	roleUnit.appId = $.trim($('#_appId').val());
	roleUnit.roleId = $.trim($('#_roleId').val());
	
	var unitList = [];
	var node;
	var unit;
	for(var i=0;i<nodes.length;i++){
		node = nodes[i];
		unit = {};
		unit.id = node.id;
		unit.pId = node.pId;
		unit.name = node.name;
		
		unitList[i] = unit;
	}
	roleUnit.unitList = unitList;
	roleUnit = JSON.stringify(roleUnit);
	console.log('roleUnit = '+roleUnit);
	
	isSubmiting_roleUnit = true;
	
	 $.ajax({
			url:baseUrl+'/role/save/role/app/units',
			type:'post',
			data:roleUnit,
			dataType:'json',
			contentType : 'application/json;charset=utf-8', //设置请求头信息
			success:function(result){
				if(result.code == 'success'){
					successTip();
				//	setInterval("parent.layer.close(parent.layer.getFrameIndex(window.name))",2000);
					 
				}else{
					errorTip();
				}
				isSubmiting_roleUnit = false;
			},
			error:function(res){
				console.log(res);
			}
		}); 
	 
	
}

	var code;
	
	function setCheck() {
		var zTree = $.fn.zTree.getZTreeObj("unitTree"),
		type = { "Y" : "ps", "N" : "s" };
		zTree.setting.check.chkboxType = type;
		showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "' + type.N + '" };');
	}
	function showCode(str) {
		if (!code) code = $("#code");
		code.empty();
		code.append("<li>"+str+"</li>");
	}
	
	

</script>
</html>
